package com.xgk.boot.module.core.controller.admin.channel;


import com.xgk.boot.framework.common.core.KeyValue;
import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.channel.vo.sp.*;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageReqVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageRespVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiSaveReqVO;
import com.xgk.boot.module.core.dal.entity.channel.ApiServiceDO;
import com.xgk.boot.module.core.dal.entity.channel.ServiceProviderDO;
import com.xgk.boot.module.core.dal.iservice.ServiceProviderAPIService;
import com.xgk.boot.module.core.dal.iservice.ServiceProviderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/admin/sp")
@Validated
public class ServiceProviderController {

    @Resource
    private ServiceProviderService spService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商")
    public CommonResult<Long> createSp(@Valid @RequestBody SpSaveReqVO createReqVO) {
        return success(spService.createSp(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "修改供应商")
    public CommonResult<Boolean> updateSp(@Valid @RequestBody SpSaveReqVO updateReqVO) {
        spService.updateSp(updateReqVO);
        return success(true);
    }

    @PostMapping("/active")
    @Operation(summary = "启用供应商")
    public CommonResult<Boolean> activeSp(@RequestParam Long asId, @RequestParam Integer asStatus) {
        spService.activeSp(asId, asStatus);
        return success(true);
    }


    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
    public CommonResult<PageResult<SpPageRespVO>> getSpPage(SpPageReqVO pageReqVO) {
        PageResult<ServiceProviderDO> pageResult = spService.getSpPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpPageRespVO.class));
    }

    @Operation(summary = "/查询供应商列表")
    @GetMapping(value = "/selectList")
    public CommonResult<List<KeyValue>> getSpSelectList() {
        List<ServiceProviderDO> list = spService.getSpSelectList();
        List<KeyValue> maps =
                list.stream().map(d -> new KeyValue<>(d.getSpId(), d.getSpName() + "[" + d.getSpCode() + "]")).collect(Collectors.toList());
        return success(maps);
    }
    @GetMapping("/queryApiRelations")
    @Operation(summary = "获取供应商关联API")
    public CommonResult<PageResult<ApiPageRespVO>> queryApiRelations(SpApiRelaReqVO pageReqVO) {
        PageResult<ApiServiceDO> pageResult = spService.queryApiRelations(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApiPageRespVO.class));
    }

    @PostMapping("/unbindApi")
    @Operation(summary = "供应商API解绑")
    public CommonResult<Boolean> unbindApi(@RequestParam Long spId, @RequestParam Long asId) {
        spService.unbindApi(spId, asId);
        return success(true);
    }

    @PostMapping("/bindApi")
    @Operation(summary = "供应商API绑定")
    public CommonResult<Boolean> bindApi(@RequestParam Long spId, @RequestParam Long asId) {
        spService.bindApi(spId, asId);
        return success(true);
    }

    @Operation(summary = "/查询API列表")
    @GetMapping(value = "/selectListNotInSp")
    public CommonResult<List<KeyValue>> getApiSelectListNotInSp(@RequestParam Long spId) {
        List<ApiServiceDO> list = spService.getApiSelectListNotInSp(spId);
        List<KeyValue> maps = list.stream().map(d -> new KeyValue<>(d.getAsId(), d.getAsName() + "[" + d.getAsCode() + "]")).collect(Collectors.toList());
        return success(maps);
    }

    @GetMapping("/provider_tree")
    public CommonResult<List<SpSelectNode>> getProviderApiTree() {
        return success(spService.buildProviderTree());
    }
}
